package com.isnotlocus.gulimail.ware.service.impl;

import com.isnotlocus.common.utils.constant.WareConstant;
import com.isnotlocus.gulimail.ware.PurchaseMergeVo;
import com.isnotlocus.gulimail.ware.entity.PurchaseDetailEntity;
import com.isnotlocus.gulimail.ware.service.*;
import com.isnotlocus.gulimail.ware.vo.PurchaseDetailVo;
import com.isnotlocus.gulimail.ware.vo.PurchaseDoneVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.isnotlocus.common.utils.PageUtils;
import com.isnotlocus.common.utils.Query;

import com.isnotlocus.gulimail.ware.dao.PurchaseDao;
import com.isnotlocus.gulimail.ware.entity.PurchaseEntity;
import org.springframework.transaction.annotation.Transactional;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {


    @Autowired
    PurchaseDetailService purchaseDetailService;

    @Autowired
    WareSkuService wareSkuService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils GetUnreceiveList(Map<String, Object> params) {
        QueryWrapper<PurchaseEntity> wrapper = new QueryWrapper<PurchaseEntity>();
        wrapper.eq("status", 0).or().eq("status", 1);
        IPage<PurchaseEntity> page = this.page(new Query<PurchaseEntity>().getPage(params), wrapper);

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void purchaseMerge(PurchaseMergeVo purchaseMergeVo) {
        List<Long> list = purchaseMergeVo.getItems();
        list.forEach(id -> {
            PurchaseDetailEntity pu = purchaseDetailService.getById(id);
            if (pu.getStatus() == WareConstant.PurchaseEnum.CREATED.getCode() || pu.getStatus() == WareConstant.PurchaseEnum.ASSIGEND.getCode()) {
                Long PurchaseId = purchaseMergeVo.getPurchaseId();
                if (PurchaseId == null) {
                    //没有提交ID的保存方法，先创建PurchaseEntity
                    PurchaseEntity purchaseEntity = new PurchaseEntity();
                    purchaseEntity.setStatus(WareConstant.AttrEnum.CREATED.getCode());
                    purchaseEntity.setCreateTime(new Date());
                    purchaseEntity.setUpdateTime(new Date());
                    this.save(purchaseEntity);
                    PurchaseId = purchaseEntity.getId();
                }
                Long finalPurchaseId = PurchaseId;
                List<PurchaseDetailEntity> details = list.stream().map((item) -> {
                    PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
                    purchaseDetailEntity.setId(item);
                    purchaseDetailEntity.setPurchaseId(finalPurchaseId);
                    purchaseDetailEntity.setStatus(WareConstant.PurchaseEnum.ASSIGEND.getCode());
                    return purchaseDetailEntity;
                }).collect(Collectors.toList());
                purchaseDetailService.updateBatchById(details);
                PurchaseEntity purchaseEntity = new PurchaseEntity();
                purchaseEntity.setId(PurchaseId);
                purchaseEntity.setUpdateTime(new Date());
                this.updateById(purchaseEntity);
            }
        });


    }

    @Override
    public void receivedPurchase(List<Long> ids) {
        //1.确认当前采购单的状态是新建或者是已分配的采购单
        List<PurchaseEntity> list = ids.stream().map((item) -> {
            PurchaseEntity byIds = this.getById(item);
            return byIds;
        }).filter((item) -> {
            if (item.getStatus() == WareConstant.AttrEnum.CREATED.getCode() || item.getStatus() == WareConstant.AttrEnum.ASSIGEND.getCode()) {
                return true;
            }
            return false;
        }).map((item) -> {
            item.setStatus(WareConstant.AttrEnum.RECEIVE.getCode());
            item.setUpdateTime(new Date());
            return item;
        }).collect(Collectors.toList());
        //2.改变采购单单状态
        this.updateBatchById(list);
        //3.改变采购需求的状态
        list.forEach(item -> {
            List<PurchaseDetailEntity> purchaseDetailEntity = purchaseDetailService.ListDetailPurchase(item.getId());
            List<PurchaseDetailEntity> PCDL = purchaseDetailEntity.stream().map(map -> {
                PurchaseDetailEntity p = new PurchaseDetailEntity();
                p.setId(map.getId());
                p.setStatus(WareConstant.PurchaseEnum.BUYING.getCode());
                return p;
            }).collect(Collectors.toList());
            purchaseDetailService.updateBatchById(PCDL);

        });
    }
    @Transactional
    @Override
    public void DonPurchase(PurchaseDoneVo vo) throws IOException {
        //1.修改采购需求的状态，并根据状态添加库存
        Boolean flag = true;
        List<PurchaseDetailVo> vos = vo.getItem();
        List<PurchaseDetailEntity> updates = new ArrayList<PurchaseDetailEntity>();
        for (PurchaseDetailVo PDV : vos) {
            PurchaseDetailEntity detailEntity = new PurchaseDetailEntity();
            if (PDV.getStatus() == WareConstant.PurchaseEnum.HASHERROR.getCode()) {
                flag = false;
                detailEntity.setStatus(PDV.getStatus());
            } else {
                detailEntity.setStatus(WareConstant.PurchaseEnum.FINISH.getCode());
                //采购需求完成，更新库存
                PurchaseDetailEntity purchaseDetailEntity = purchaseDetailService.getById(PDV.getItemId());
                wareSkuService.updateWare(purchaseDetailEntity);
            }
            detailEntity.setId(PDV.getItemId());
            updates.add(detailEntity);
        }
        purchaseDetailService.updateBatchById(updates);
        //2.修改采购单的状态
        PurchaseEntity purchaseEntity = new PurchaseEntity();
        if (!flag) {
            purchaseEntity.setId(vo.getId());
            purchaseEntity.setStatus(WareConstant.AttrEnum.HASHERROR.getCode());
        } else {
            purchaseEntity.setId(vo.getId());
            purchaseEntity.setStatus(WareConstant.AttrEnum.FINISH.getCode());
        }
        this.updateById(purchaseEntity);

    }

}